package com.anyou.common.utils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class TimeUtil {

    private static DateTimeFormatter dt0 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * localDateTimel转字符串String
     * @param dateTime
     * @param pattern "yyyy-MM-dd HH:mm:ss"
     * @return
     */
    public static String localDateTimeToString(LocalDateTime dateTime,String pattern) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
        String date = dtf.format(dateTime);
        return date;
    }

    /**
     * Stringl转localDateTime
     * @param dateTime
     * @param pattern "yyyy-MM-dd HH:mm:ss"
     * @return
     */
    public static LocalDateTime stringToLocalDateTime(String dateTime,String pattern) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
        LocalDateTime date = LocalDateTime.parse(dateTime, dtf);
        return date;
    }

    /**
     * Date转String
     * @param date
     * @param pattern "yyyy-MM-dd HH:mm:ss"
     * @return
     */
    public static String dateToString(Date date,String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String dateTime = sdf.format(date);;
        return dateTime;
    }

    /**
     * Date转localDateTime
     * @param date
     * @param pattern "yyyy-MM-dd HH:mm:ss"
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date,String pattern) {
        String time = dateToString(date,pattern);
        LocalDateTime localDateTime = stringToLocalDateTime(time, pattern);
        return localDateTime;
    }


}
